//
//  Tool.m
//  MyCalendar
//
//  Created by ly on 15-3-8.
//  Copyright (c) 2015年 ly. All rights reserved.
//

#import "Tool.h"

#import "Tool.h"

@interface Tool(){
    
    NSCalendar *_calendar;
    NSDateComponents *_nowDateComps;
    NSDateFormatter *_dateFormatter;
    NSDate *_nowDate;
}
- (BOOL)isLoopYear:(NSInteger)year;

@end


@implementation Tool

- (instancetype)init
{
    self = [super init];
    if (self) {
        _calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        _nowDate = [NSDate date];
        _dateFormatter = [[NSDateFormatter alloc] init];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd"];
        _nowDateComps = [_calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:_nowDate];
    }
    return self;
}

//返回当前年
- (NSInteger)getYear{
    
    return [_nowDateComps year];
}

//返回当前月
- (NSInteger)getMonth{
    
    return [_nowDateComps month];
}

//返回当前日
- (NSInteger)getDay{
    
    return [_nowDateComps day];
}

//获得某个月的第一天是星期几
- (NSInteger)getWeekOfFirstDayOfMonth:(NSInteger)year withMonth:(NSInteger)month{
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSString *firstWeekDayMonth = [[NSString alloc] initWithFormat:@"%d",year];
    firstWeekDayMonth = [firstWeekDayMonth stringByAppendingString:[[NSString alloc]initWithFormat:@"%s","-"]];
    firstWeekDayMonth = [firstWeekDayMonth stringByAppendingString:[[NSString alloc]initWithFormat:@"%d",month]];
    firstWeekDayMonth = [firstWeekDayMonth stringByAppendingString:[[NSString alloc]initWithFormat:@"%s","-"]];
    firstWeekDayMonth = [firstWeekDayMonth stringByAppendingString:[[NSString alloc]initWithFormat:@"%d",1]];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
     NSDate *weekOfFirstDayOfMonth = [dateFormatter dateFromString:firstWeekDayMonth];
    
    NSDateComponents *newCom = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:weekOfFirstDayOfMonth];

    return  [newCom weekday] - 1;
}

//返回一个月有多少天
- (NSInteger)getDaysOfMonth:(NSInteger)year withMonth:(NSInteger)month{
    
    NSInteger days = 0;
    //1,3,5,7,8,10,12
    NSArray *bigMoth = [[NSArray alloc] initWithObjects:@"1",@"3",@"5",@"7",@"8",@"10",@"12", nil];
    //4,6,9,11
    NSArray *milMoth = [[NSArray alloc] initWithObjects:@"4",@"6",@"9",@"11", nil];
    
    if ([bigMoth containsObject:[[NSString alloc] initWithFormat:@"%ld",(long)month]]) {
        days = 31;
    }else if([milMoth containsObject:[[NSString alloc] initWithFormat:@"%ld",(long)month]]){
        days = 30;
    }else{
        if ([self isLoopYear:year]) {
            days = 29;
        }else
            days = 28;
    }
    return days;
}

//判断是否是闰年
- (BOOL)isLoopYear:(NSInteger)year{
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        return true;
    }else
        return NO;
}

@end
